program elgcreate

  implicit none


  integer ierr, num_procs, my_id
  


  call start()
  stop

end program elgcreate


subroutine start()
  use mod_elglike
  use mod_elglike_element
  use mod_elgdata
  use mod_lutil
  type(t_matrix)  :: matrix, invmat
  type(t_elgdata) :: elgdata, elgdataTest
  integer :: i,j, p
  real*8, dimension(10) :: params
  real*8 :: l
  character(len=128) :: pfile

  ! initial parameters

  params(1) = 0.3 ! omega m
  params(2) = 0.01 ! 0.01 omega_nu
  params(3) = -1 ! w0
  params(4) = 0.01 ! wa
  params(5) = 1.0 ! amplitude

  pfile = "elgmomc.ini"


  call lutil_get_parameter(21, pfile, "lmax", par_int=global_lmax)
  call lutil_get_parameter(21, pfile, "zbins", par_int=zbins)


  call elgdata_initialize(elgdata, global_lmax_data)

  write(*,*) "Initializing for ", global_lmax_data, "data"
  do i=1, global_lmax
     l = real(i) 
     p = (i*100)/global_lmax
     params(6) = l
     call elglike_element_setup_matrix(elgdata%elements(i), params)
    ! call matrix_print(elgdata%elements(i)%array)
     call elglike_element_calculate_determinant(elgdata%elements(i), l)
     write(*,*) "Done calculated element", p,"% with det ", elgdata%elements(i)%determinant
  end do

  write(*,*) "Done!"
!  call matrix_invert(matrix, invmat)
  call elgdata_save(elgdata,"elgdata.dat")



  call elgdata_load(elgdataTest, "elgdata.dat")


  do i=1, lmax
     write(*,*) elgdata%elements(i)%determinant, " vs ", elgdataTest%elements(i)%determinant
  end do

end subroutine start

  

